<?php
namespace app\admin\controller;

use app\admin\model\Comment;
use app\admin\model\Intro;
use app\admin\model\Judge;
use app\admin\model\Team;
use think\Db;
use think\facade\Session;
use think\Request;

class Index
{
    public function index()
    {
        if (!Session::has('admin'))  return redirect('@admin/login');
        return redirect('@admin/comments');
    }

    public function upload(Request $request)
    {
        $file = $request->file('file');
        dump($file);
        return json(['code' => 0, 'msg' => 'success']);
    }

    public function login()
    {
        if (Session::has('admin'))  return redirect('@admin');
        return view();
    }

    public function login_a(Request $request)
    {
        $user = Db::table("admin")->find();
        if ($user['username'] == $request->param('username')
            && $user['password'] == $request->param('password')) {
            Session::set('admin', $user);
            return json(['code' => 0]);
        } else {
            return json(['code' => 1, 'msg' => '用户名或密码错误']);
        }
    }

    public function logout_a()
    {
        if (Session::has('admin'))  {
            Session::delete('admin');
            return redirect('@admin/login');
        }
    }

    public function intro()
    {
        $intro = Intro::get(1);
        return view('', compact('intro'));
    }

    public function intro_update(Request $request)
    {
        $data = $request->except('_method');
        $data['updated_at'] = time();
        Intro::where('id', 1)->update($data);
        return redirect('@admin/intro');
    }

    public function comments()
    {
        $comments = Comment::all();
        return view('', compact('comments'));
    }

    public function comment_show($id)
    {
        Comment::where('id', $id)->update([
            'is_show' => 1
        ]);
        return redirect('@admin/comments');
    }

    public function comment_hide($id)
    {
        Comment::where('id', $id)->update([
            'is_show' => 0
        ]);
        return redirect('@admin/comments');
    }

    public function comment_delete($id)
    {
        Comment::destroy($id);
        return redirect('@admin/comments');
    }

    public function teams()
    {
        $teams = Team::all();
        return view('', compact('teams'));
    }

    public function team_create()
    {
        return view();
    }

    public function team_store(Request $request)
    {

        $data = $request->except('file');
        $data['created_at'] = time();
        Team::create($data);
        return redirect('@admin/teams');
    }

    public function team_edit(Request $request, $id)
    {
        $team = Team::find($id);
        return view('', compact('team'));
    }

    public function team_update(Request $request, $id)
    {
        Team::where('id', $id)->update([
            'name' => $request->param('name'),
            'facepic' => $request->param('facepic'),
            'content' => $request->param('content'),
        ]);
        return redirect('@admin/teams');
    }

    public function team_delete($id)
    {
        Team::destroy($id);
        return redirect('@admin/teams');
    }

    public function judges()
    {
        $judges = Judge::all();
        return view('', compact('judges'));
    }

    public function judge_create()
    {
        return view();
    }

    public function judge_edit($id)
    {
        $judge = Judge::find($id);
        return view('', compact('judge'));
    }

    public function judge_update(Request $request, $id)
    {
        Judge::where('id', $id)->update([
            'account' => $request->param('account'),
            'password' => $request->param('password'),
            'facepic' => $request->param('facepic'),
            'name' => $request->param('name'),
            'desc' => $request->param('desc')
        ]);
        return redirect('@admin/judges');
    }

    public function judge_store(Request $request)
    {
        Judge::create($request->except('file'));
        return redirect('@admin/judges');
    }

    public function judge_delete($id)
    {
        Db::table('judges')->where('id', $id)->delete();
        return redirect('@admin/judges');
    }
}
